import { createRouter, createWebHistory } from 'vue-router'
import Home from '../views/Home.vue'
import News from '../views/News.vue'
import Detection from '../views/Detection.vue'
import About from '../views/About.vue'
import DigitalMap from '../views/DigitalMap.vue'
import QualityComparison from '../views/QualityComparison.vue'
import QualityEvaluation from '../views/QualityEvaluation.vue'
import Login from '../views/Login.vue'
import Register from '../views/Register.vue'
import ForgotPassword from '../views/ForgotPassword.vue'
import Admin from '../views/Admin.vue'
import User from "../views/User.vue";
import TestElement from "../views/TestElement.vue";

const routes = [
  { path: '/', name: 'Home', component: Home },
  { path: '/user', name: 'User', component: User },
  { path: '/news', name: 'News', component: News },
  { path: '/detection', name: 'Detection', component: Detection },
  { path: '/about', name: 'About', component: About },
  { path: '/digital-map', name: 'DigitalMap', component: DigitalMap, meta: { requiresAuth: true }, props: { data: null } },
  { path: '/quality-comparison', name: 'QualityComparison', component: QualityComparison, meta: { requiresAuth: true }, props: { data: null } },
  { path: '/quality-evaluation', name: 'QualityEvaluation', component: QualityEvaluation, meta: { requiresAuth: true }, props: { data: null } },
  { path: '/login', name: 'Login', component: Login },
  { path: '/register', name: 'Register', component: Register },
  { path: '/forgot-password', name: 'ForgotPassword', component: ForgotPassword },
  { path: '/admin', name: 'Admin', component: Admin, meta: { requiresAuth: true } },
  { path: '/test-element', name: 'TestElement', component: TestElement }
]

const router = createRouter({
  history: createWebHistory(process.env.BASE_URL),
  routes
})

// 全局前置守卫
router.beforeEach((to, from, next) => {
  if (to.meta && to.meta.requiresAuth) {
    const currentUser = localStorage.getItem('currentUser')
    if (!currentUser) {
      return next({ path: '/login', query: { redirect: to.fullPath } })
    }
  }
  next()
})

export default router